package my.georings.data;

import java.io.IOException;
import java.io.Serializable;

import android.location.Location;
import android.net.Uri;

import com.google.android.maps.GeoPoint;

public class MyLocation extends Object implements Serializable{
	
	private static final long serialVersionUID = 1L;
	
	private boolean ON = true;
	private int id = -1;
	private String name = null;
	private GeoPoint point = null;
	private boolean changeVolume = false;
	private int volume = -1;
	private boolean vibra = false;
	private boolean changeRing = false;
	private Uri ring = null;
	private boolean mute = false;
	private int radius = 100;
	
	private Location location = null;
	private MyLocationsManager parentManager = null;
	
	public MyLocation(){
	}
	
	
	public MyLocation(int id, String name,
			GeoPoint point, int radius) {
		super();
		this.id = id;
		this.name = name;
		this.point = point;
		this.ON = true;
		this.radius = radius;
	}
	
	
	public MyLocation(int id, String name,
			GeoPoint point, boolean changeVolume,
			int volume, boolean vibra, boolean changeRing, Uri ring, boolean mute, int radius) {
		super();
		this.id = id;
		this.name = name;
		this.point = point;
		this.changeVolume = changeVolume;
		this.volume = volume;
		this.vibra = vibra;
		this.changeRing = changeRing;
		this.ring = ring;
		this.ON = true;
		this.mute = mute;
		this.radius = radius;
	}
	
	public void clear(){
		this.name = null;
		this.point = null;
		this.changeVolume = false;
		this.volume = -1;
		this.vibra = false;
		this.changeRing = false;
		this.ring = null;
		this.ON = true;
		this.mute = false;
		
		this.location = null;
	}
	
	public boolean isValid(boolean makeValid){
		// aby byl poprawny:
		// - nazwa
		// - punkt
		// - przy zmianie dzownka musi byc podany dzownek
		
		if(		this.name != null && 
				this.point != null && 
				(changeRing ? ring != null : true) &&
				(changeVolume ? volume >= 0 : true)){
			// usuniecie niepotrzebnych elementow
			if(!changeRing){
				ring = null;
			}
			if(!changeVolume){
				volume = -1;
			}
			
			if(mute){
				changeVolume = false;
				changeRing = false;
				volume = -1;
				ring = null;
			}
			
			return true;
		}
		return false;
	}
	
	
	private void writeObject(java.io.ObjectOutputStream out) throws IOException{
		out.writeInt(id);
		out.writeUTF(name);
		out.writeInt(point.getLatitudeE6());
		out.writeInt(point.getLongitudeE6());
		out.writeBoolean(changeVolume);
		out.writeInt(volume);
		out.writeBoolean(vibra);
		out.writeBoolean(changeRing);
//		Log.d("GeoPhoneRings", "for: " + name + " path: " + (ring == null ? "" : ring.toString()));
		out.writeUTF(ring == null ? "" : ring.toString());
		out.writeBoolean(ON);
		out.writeBoolean(mute);
		out.writeInt(radius);
	}

	
	private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException{
		String read;
		
		id = in.readInt();
		name = in.readUTF();
		
		point = new GeoPoint(in.readInt(), in.readInt());
		
		changeVolume = in.readBoolean();
		volume = in.readInt();
		
		vibra = in.readBoolean();
		
		changeRing = in.readBoolean();
		read = in.readUTF();
		if(read == ""){
			ring = null;
		}else{
			ring = Uri.parse(read);
		}
		
//		Log.d("GeoPhoneRings", "DE: for: " + name + " path: " + (ring == null ? "" : ring.toString()));
		ON = in.readBoolean();
		mute = in.readBoolean();
		radius = in.readInt();
		
		if(radius == 50)
			radius = 100;
		else if(radius == 100)
			radius = 200;
		else
			radius = 300;
	}
	
	
	public MyLocation copy(){
		MyLocation theCopy = new MyLocation();
		theCopy.setId(this.id);
		theCopy.setName(this.name);
		theCopy.setChangeRing(this.changeRing);
		theCopy.setVibra(vibra);
		theCopy.setChangeVolume(changeVolume);
		theCopy.setRing(ring);
		theCopy.setVolume(volume);
		theCopy.setON(this.ON);
		theCopy.setPoint(new GeoPoint(point.getLatitudeE6(), point.getLongitudeE6()));
		theCopy.setMute(mute);
		theCopy.setParentManager(parentManager);
		theCopy.setRadius(radius);
		return theCopy;
	}
	
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public boolean isChangeVolume() {
		return changeVolume;
	}
	public void setChangeVolume(boolean changeVolume) {
		this.changeVolume = changeVolume;
	}
	public int getVolume() {
		return volume;
	}
	public void setVolume(int volume) {
		this.volume = volume;
	}
	public boolean isVibra() {
		return vibra;
	}
	public void setVibra(boolean vibra) {
		this.vibra = vibra;
	}
	public boolean isChangeRing() {
		return changeRing;
	}
	public void setChangeRing(boolean changeRing) {
		this.changeRing = changeRing;
	}
		
	public Uri getRing() {
		return ring;
	}

	public void setRing(Uri ring) {
		this.ring = ring;
	}

	public boolean isON() {
		return ON;
	}

	public void setON(boolean oN) {
		ON = oN;
	}
	
	public void setONnotifyParent(boolean on){
		ON = on;
		if(parentManager != null){
			parentManager.changeLocationOnOff(this);
		}
	}

	public GeoPoint getPoint() {
		return point;
	}

	public void setPoint(GeoPoint point) {
		this.point = point;
		location = new Location("");
		location.setLatitude((double)point.getLatitudeE6()/1000000.0);
		location.setLongitude((double)point.getLongitudeE6()/1000000.0);
		location.setTime(System.currentTimeMillis());
	}

	public boolean isMute() {
		return mute;
	}

	public void setMute(boolean mute) {
		this.mute = mute;
	}


	public Location getLocation() {
		if(location == null){
			location = new Location("");
			location.setLatitude((double)point.getLatitudeE6()/1000000.0);
			location.setLongitude((double)point.getLongitudeE6()/1000000.0);
		}
		return location;
	}

	
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (ON ? 1231 : 1237);
		result = prime * result + (changeRing ? 1231 : 1237);
		result = prime * result + (changeVolume ? 1231 : 1237);
		result = prime * result + id;
		result = prime * result + (mute ? 1231 : 1237);
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((point == null) ? 0 : point.hashCode());
		result = prime * result + ((ring == null) ? 0 : ring.hashCode());
		result = prime * result + (vibra ? 1231 : 1237);
		result = prime * result + volume;
		return result;
	}


	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		MyLocation other = (MyLocation) obj;
		if (ON != other.ON)
			return false;
		if (changeRing != other.changeRing)
			return false;
		if (changeVolume != other.changeVolume)
			return false;
		if (id != other.id)
			return false;
		if (mute != other.mute)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (point == null) {
			if (other.point != null)
				return false;
		} else if (!point.equals(other.point))
			return false;
		if (ring == null) {
			if (other.ring != null)
				return false;
		} else if (!ring.equals(other.ring))
			return false;
		if (vibra != other.vibra)
			return false;
		if (volume != other.volume)
			return false;
		return true;
	}


	public MyLocationsManager getParentManager() {
		return parentManager;
	}


	public void setParentManager(MyLocationsManager parentManager) {
		this.parentManager = parentManager;
	}


	public int getRadius() {
		return radius;
	}


	public void setRadius(int radius) {
		this.radius = radius;
	}
	
	

}
